<blockquote>
    <p>Reids插件 @EnableRedisPlugin 注解, 默认使用缺省配置, 配置后可以注入</p>
    <footer>
        <p><code>String host</code>: Redis 服务地址</p>
        <p><code>String port</code>: Redis端口号</p>
        <p><code>String database</code>: Redis数据库</p>
        <p><code>String password</code>: Redis链接密码</p>
        <p>缺省配置</p>
        <p>#redis</p>
        <p>ants.redis.host=127.0.0.1</p>
        <p>ants.redis.port=6379</p>
        <p>ants.redis.database=0</p>
        <p>ants.redis.password=123456</p>
        <p>注解配置</p>
        @EnableRedisPlugin(host="127.0.0.1", port="6379", database = "0", password ="123456")
    </footer>
</blockquote>
<blockquote>
    <p>使用Redis时需要加入Maven依赖</p>
</blockquote>
<pre class="code brush:xml">
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>
</pre>
<blockquote>
    <p>Redis配置文件 <code>application.properties</code></p>
</blockquote>
<pre class="code brush:plain">
#redis

ants.redis.host=127.0.0.1

ants.redis.port=6379

ants.redis.database=0

ants.redis.password=123456
</pre>
<blockquote>
    <p>通过注入获取RedisTpl对象</p>
    <footer style="color:red;">启动类必须要配置 @EnableRedisPlugin 注解</footer>
    <footer style="color:red;">当存储的是普通字符串时, 需要通过getStr获取;如果是Json字符串可以直接get获取</footer>
</blockquote>
<pre class="code brush:java">
package cn.jants.demos;


import cn.jants.common.annotation.action.Controller;
import cn.jants.common.annotation.action.GET;
import cn.jants.common.annotation.boot.PropertyConfiguration;
import cn.jants.common.annotation.plugin.EnableEhcachePlugin;
import cn.jants.common.annotation.plugin.EnableRedisPlugin;
import cn.jants.common.annotation.plugin.EnableSQLMapPlugin;
import cn.jants.common.annotation.service.Application;
import cn.jants.common.annotation.service.Autowired;
import cn.jants.common.bean.JsonMap;
import cn.jants.core.context.AppConfiguration;
import cn.jants.core.startup.JTomcat;
import cn.jants.plugin.cache.RedisTpl;
import cn.jants.plugin.tool.EhCacheTool;
import cn.jants.plugin.tool.RedisTool;
import cn.jants.restful.render.Json;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.Map;


/**
 * Tomcat 方式启动
 *
 * @author MrShun
 * @version 1.0
 * @Date 2017/12/20
 */
@Application
@PropertyConfiguration
@EnableRedisPlugin
@Controller
public class DemosApplication extends AppConfiguration {

    @Autowired
    private RedisTpl redisTpl;

    @GET("/redis/set")
    public void set(){
        //存储普通字符串数据
        redisTpl.set("strKey", "测试Redis存储普通字符串....");
        //存储Json格式字符串数据
        JsonMap jsonMap = JsonMap.newJsonMap();
        jsonMap.set("name", "MrShun").set("age", 25);
        redisTpl.set("jsonKey", jsonMap.toJsonString());
    }

    /**
     * 当存储的是普通字符串时, 需要通过getStr获取;
     * 如果是Json字符串可以直接get获取
     * @return
     */
    @GET("/redis/get")
    public Map get(){
        //取普通字符串
        String value = redisTpl.getStr("strKey");
        //获取Json字符串
        JSONObject jsonObject = redisTpl.get("jsonKey");
        return Json.success(jsonObject.getString("name"));
    }

    public static void main(String[] args) {
        JTomcat.run(DemosApplication.class, 80, "/demo");
    }
}
</pre>
<blockquote>
    <p>通过工具类直接RedisTpl对象</p>
    <footer style="color:red;">启动类不需要配置 @EnableRedisPlugin 注解</footer>
</blockquote>
<pre class="code brush:java">
package cn.jants.demos;


import cn.jants.common.annotation.action.Controller;
import cn.jants.common.annotation.action.GET;
import cn.jants.common.annotation.boot.PropertyConfiguration;
import cn.jants.common.annotation.plugin.EnableEhcachePlugin;
import cn.jants.common.annotation.plugin.EnableRedisPlugin;
import cn.jants.common.annotation.plugin.EnableSQLMapPlugin;
import cn.jants.common.annotation.service.Application;
import cn.jants.common.annotation.service.Autowired;
import cn.jants.common.bean.JsonMap;
import cn.jants.core.context.AppConfiguration;
import cn.jants.core.startup.JTomcat;
import cn.jants.plugin.cache.RedisTpl;
import cn.jants.plugin.tool.EhCacheTool;
import cn.jants.plugin.tool.RedisTool;
import cn.jants.restful.render.Json;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.Map;


/**
 * Tomcat 方式启动
 *
 * @author MrShun
 * @version 1.0
 * @Date 2017/12/20
 */
@Application
@PropertyConfiguration
@Controller
public class DemosApplication extends AppConfiguration {


    @GET("/redis/set")
    public void set(){
        RedisTpl redisTpl = RedisTool.getRedis();
        //存储普通字符串数据
        redisTpl.set("strKey", "测试Redis存储普通字符串....");
        //存储Json格式字符串数据
        JsonMap jsonMap = JsonMap.newJsonMap();
        jsonMap.set("name", "MrShun").set("age", 25);
        redisTpl.set("jsonKey", jsonMap.toJsonString());
    }

    /**
     * 当存储的是普通字符串时, 需要通过getStr获取;
     * 如果是Json字符串可以直接get获取
     * @return
     */
    @GET("/redis/get")
    public Map get(){
        RedisTpl redisTpl = RedisTool.getRedis();
        //取普通字符串
        String value = redisTpl.getStr("strKey");
        //获取Json字符串
        JSONObject jsonObject = redisTpl.get("jsonKey");
        return Json.success(jsonObject.getString("name"));
    }

    public static void main(String[] args) {
        JTomcat.run(DemosApplication.class, 80, "/demo");
    }
}
</pre>
<script type="text/javascript">
    $(function () {
        SyntaxHighlighter.highlight();
        $(".toolbar").remove();
    });
</script>